package com.gxaedu.rbac.controller;

import com.gxaedu.rbac.dto.ResultData;
import com.gxaedu.rbac.pojo.User;
import com.gxaedu.rbac.service.RbacService;
import com.gxaedu.rbac.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author:Teacher黄
 * @date:Created at 2020/04/20
 */
@Controller
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private UserService userService;

    @Autowired
    private RbacService rbacService;

    /**
     * 登陆页面
     * @return
     */
    @GetMapping("/page")
    public String loginPage(){
        return "login";
    }


    /**
     * 登陆操作
     * @param user
     * @return
     */
    @PostMapping("/do")
    @ResponseBody
    public ResultData loginDo(User user, HttpServletRequest request){
        // 数据验证 : 自行加上
        return userService.login(user,request);
    }


    /**
     * 退出登陆的方法
     * @return
     */
    @GetMapping("/out")
    public String logout(HttpServletRequest request){
        System.out.println(request);
        // 调用RbacService 清空缓存
        rbacService.cleanCache(request);
        // 将session数据置为null
        request.getSession().setAttribute("user",null);
        request.getSession().setAttribute("userName",null);
        // 重定向到登陆页面
        return "redirect:/login/page";
    }


}
